package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.LogDao;
import com.pojo.User;
import com.service.LogService;
import com.service.UserService;
import com.util.sqlSessionFactory;

@Controller
public class UserController {
	UserService service1 = new UserService();
	LogService service2 = new LogService();
    /*注册*/
	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2) {

		List<User> list = service1.query(account);
		if (!password.equals(password2)) {
			return "SS";
		}
		if ("1".equals(list.get(0).getCounter().toString())) {
			return "ERR";
		}
		System.out.println(list.get(0).getCounter());
		service1.add(account, password, account);
		// Map<String, Object> map = dao.lastId();
		// dao1.insert(map.get("id").toString(), "注册", "0");
		// Map id = dao1.queryid(account);
		// dao1.insert(id.get("id").toString(), "注册",
		// id.get("id").toString(),"2012-12-12","12:12:12");
		return "OK";
	}
     /*登录*/
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String queryone(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		User list1 = service1.queryone(account, password);
		// User list1 = service1.lastId();
		// System.out.println(list);
		// System.out.println(password);
		if (!"1".equals(list1.getCounter())) {
			return "ERR";
		}
		service2.insert(list1.getId(), "登录", "0");
		// System.out.println(password);
		response.addCookie(new Cookie("ID", list1.getId()));
		return "OK";
	}
/*查询用户名*/
	@RequestMapping(value = "/querynickname.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> querynickname(
			@CookieValue(value = "ID", defaultValue = "") String userId,
			HttpServletResponse response) {
		Map<String, Object> list = service1.querynickname(userId);
		return list;
	}
/*记录退出操作*/
	@RequestMapping(value = "/quit.action", method = RequestMethod.POST)
	@ResponseBody
	public String quit(
			@CookieValue(value = "ID", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = sqlSessionFactory.getSqlSession();
		service2.insert(userId, "退出", "0");
		response.addCookie(new Cookie("user", null));
		session.commit();
		return "OK";
	}
}
